﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Transactions;

namespace Pku.BookStore.Web.Controllers
{
    public class CartController : Controller
    {
        //
        // GET: /Cart/

        public ActionResult Index(int? BookID, string Act, int? Num)
        {
            if (BookID == null) BookID = 0;
            if (Num == null) Num = 0;

            List<Models.CartModels> list = new List<Models.CartModels>();
            IServices.IProductServices productDal = new Services.ProductServices();
            Model.ProductModel model = new Model.ProductModel();
            //Session["Cart"] = null;

            if (Session["Cart"] != null)
            {
                list = (List<Models.CartModels>)Session["Cart"];
            }
            //if (list == null) list = new List<Models.CartModels>();
            if (BookID != 0)
            {
                model = productDal.GetEntityById(Convert.ToInt32(BookID));
            }

            if (Act == "A")
            {
                if (list.Where(o => o.BookID == Convert.ToInt32(BookID)).Count() > 0)
                {
                    foreach (var item in list)
                    {
                        if (item.BookID == Convert.ToInt32(BookID))
                        {
                            item.Quantity += Convert.ToInt32(Num);
                        }
                    }
                }
                else
                {
                    Models.CartModels m = new Models.CartModels();
                    m.BookID = Convert.ToInt32(BookID);
                    m.BookName = model.BookName;
                    m.TruePrice = Convert.ToDouble(model.TruePrice);
                    m.Image = model.Image;
                    m.Quantity = Convert.ToInt32(Num);
                    m.PublisherID = model.PublisherId;
                    list.Add(m);
                }
                Session["Cart"] = list;
                return Redirect("/Cart");
            }
            else if (Act == "D")
            {
                if (Num != 0)
                {
                    foreach (var item in list)
                    {
                        if (item.BookID == Convert.ToInt32(BookID))
                        {
                            item.Quantity -= Convert.ToInt32(Num);
                        }
                    }
                    var ml = list.Where(o => o.Quantity == 0).FirstOrDefault();
                    list.Remove(ml);
                }
                else
                {
                    var ml = list.Where(o => o.BookID == BookID).FirstOrDefault();
                    list.Remove(ml);
                }

                Session["Cart"] = list;
                return Redirect("/Cart");
            }


            return View(list);
        }

        public ActionResult Order()
        {
            List<Models.CartModels> list = new List<Models.CartModels>();
            if (Session["Cart"] != null)
            {
                list = (List<Models.CartModels>)Session["Cart"];
            }
            ViewBag.cartList = list;
            return View();
        }

        [HttpPost]
        [UserAuthorizeAttribute]
        public ActionResult Order(Model.OrderFormModel model)
        {
            IServices.IOrderFormServices orderDal = new Services.OrderFormServices();
            IServices.IOrderDetailServices orderDetailDal = new Services.OrderDetailServices();

            if (model == null)
            {
                return Redirect("/Cart");
            }
            int OrderID = 0;
            using (var tran = new TransactionScope())
            {
                model.UserID = Convert.ToInt32(Session["UserID"]);
                model.OrderData = DateTime.Now;
                OrderID = orderDal.Add(model);
                if (OrderID != 0)
                {
                    List<Models.CartModels> list = new List<Models.CartModels>();
                    if (Session["Cart"] != null)
                    {
                        list = (List<Models.CartModels>)Session["Cart"];
                    }
                    foreach (var item in list)
                    {
                        Model.OrderDetailModel md = new Model.OrderDetailModel();
                        md.BookID = item.BookID;
                        md.OrderID = OrderID;
                        md.PublisherID = item.PublisherID;
                        md.Quantity = item.Quantity;
                        md.UnitPrice = Convert.ToDecimal(item.TruePrice);
                        orderDetailDal.Add(md);
                    }
                }
                tran.Complete();
            }
            Session["Cart"] = null;
            return Redirect(string.Format("/Cart/OrderAct/{0}", OrderID));
        }

        public ActionResult OrderAct(string ID)
        {
            ViewBag.OrderID = ID;
            return View();
        }

    }
}
